﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Bierdopje.API
{
    /// <summary>
    /// This is an representation of the Subtitle structure returned by the
    /// bierdopje API
    /// </summary>
    public class ApiSub
    {
        private string m_FileName;
        private int m_FileSize;

        public int FileSize
        {
            get { return m_FileSize; }
            set { m_FileSize = value; }
        }
        private string m_Uploader;

        public string Uploader
        {
            get { return m_Uploader; }
            set { m_Uploader = value; }
        }
        private DateTime m_PubDate;

        public DateTime PubDate
        {
            get { return m_PubDate; }
            set { m_PubDate = value; }
        }
        private int m_RepliesCount;

        public int RepliesCount
        {
            get { return m_RepliesCount; }
            set { m_RepliesCount = value; }
        }
        private int m_DownloadCount;

        public int DownloadCount
        {
            get { return m_DownloadCount; }
            set { m_DownloadCount = value; }
        }
        private string m_DownloadLink;

        public string DownloadLink
        {
            get { return m_DownloadLink; }
            set { m_DownloadLink = value; }
        }

        public string FileName
        {
            get { return m_FileName; }
            set { m_FileName = value; }
        }

        /// <summary>
        /// Generated field, uses a regex to remove the version from the filename.
        /// </summary>
        public string FileNameWithoutVersion
        {
            get
            {
                return SubHelper.RemoveVersionFromSubtitleFileName(FileName);
            }
        }

        /// <summary>
        /// Download the subtitle to a specific location
        /// </summary>
        /// <param name="location">The location to write the sub to</param>
        /// <param name="overwrite">If an existing file is present, it will be overwritten if this is set to true</param>
        public void Download(string location, bool overwrite)
        {
            if ((!overwrite) && (File.Exists(location)))
            {
                throw new Exception("Subtitle already exists: " + location);
            }

            HTTPHelper.SaveResponseToFile(DownloadLink, location, overwrite);
        }
    }
}
